package com.an.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.an.blog.admin.domain.bo.BlogSocialUserAuthBo;
import com.an.blog.admin.domain.vo.BlogSocialUserAuthVo;
import com.an.blog.mapper.BlogSocialUserAuthMapper;
import com.an.blog.service.IBlogSocialUserAuthService;
import com.an.blog.web.domain.BlogSocialUserAuth;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 社会化账号和用户中间Service业务层处理
 *
 * @author Ann
 * @date 2023-08-22
 */
@RequiredArgsConstructor
@Service
public class BlogSocialUserAuthServiceImpl implements IBlogSocialUserAuthService {

    private final BlogSocialUserAuthMapper baseMapper;


    /**
     * 新增社会化账号和用户中间
     */
    @Override
    public Boolean insertByBo(BlogSocialUserAuthBo bo) {
        BlogSocialUserAuth add = BeanUtil.toBean(bo, BlogSocialUserAuth.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改社会化账号和用户中间
     */
    @Override
    public Boolean updateByBo(BlogSocialUserAuthBo bo) {
        BlogSocialUserAuth update = BeanUtil.toBean(bo, BlogSocialUserAuth.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 查询签证官社会用户id
     *
     * @param socialUserId 社会用户id
     * @return {@link BlogSocialUserAuthVo }
     * @author: Ann
     * @createDate: 2023/08/25 20:26:29
     * @version: 1.0.0
     */
    @Override
    public BlogSocialUserAuthVo queryVoBySocialUserId(long socialUserId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<BlogSocialUserAuth>()
            .eq(BlogSocialUserAuth::getSocialUserId,socialUserId));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogSocialUserAuth entity){
        //TODO 做一些数据校验,如唯一约束
    }

}
